<?php
	if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Roles_Ctrl extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
			}
		function index($order_by  = 'created_date', $sort_order = 'asc', $current_page = 0){
			
			$results_per_page = 4;
			
			$url = CURL_URL;
			$data = array('www-command'=>'roles-all_roles','current_page'=>$current_page,'results_per_page'=>$results_per_page,'order_by'=>$order_by,'sort_order'=>$sort_order);
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response = json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
			
			$this->load->library('pagination');			
			$config['base_url'] = 'http://localhost/azindia/azindia-client/index.php/admin/roles_ctrl/index/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 6;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['roles_all_roles'] = $response;
			$data['sort_order']=$sort_order;
			$data['order_by']=$order_by;
			//echo '<pre>';print_r($response);exit;
			$this->load->view('admin/roles_view',$data);
		}
		
		/* function create_roles_page(){
			$url = "http://localhost/azindia/azindia-api/json.api";
				$data = array('www-command'=>'roles-get_rolemodules_list');
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
				$data['roles_get_rolemodules_list'] = $response;
			$this->load->view('admin/create_roles_page', $data);
		} */
		
		function create_role(){
		$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('first_name', 'Firstname', 'required|alpha|min_length[3]|max_length[20]');
				$this->form_validation->set_rules('role', 'Role', 'required'); 
				
				if($this->form_validation->run() == FALSE){
				$url = CURL_URL;
				$data = array('www-command'=>'roles-get_rolemodules_list');
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
				$data['roles_get_rolemodules_list'] = $response;
			$this->load->view('admin/create_roles_page', $data);
				}else{
				//echo '<pre>';print_r(implode(",",$this->input->post('role')));exit;
					$url = 'http://localhost/azindia/azindia-api/json.api';
					$data = array('www-command'=>'roles-create_role', 
				'first_name' => $this->input->post('first_name'),
				'module_id' => implode(",",$this->input->post('role'))
				);
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//$data['articles_create_author'] = $response;
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/roles_ctrl/index/');
				}
				
		}
		
		/* function edit_role_page(){
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('first_name', 'Firstname', 'required|alpha|min_length[3]|max_length[20]');
				$this->form_validation->set_rules('role', 'Role', 'required'); 
				
				if($this->form_validation->run() == FALSE){}
				 $this->load->model('admin_roles_model');
				$data = array('www-command'=>'roles-get_role','role_id'=>$_SESSION['role_id']);
				$roles_get_role = $this->admin_roles_model->role_data($data);
				$data = array('www-command'=>'roles-get_rolemodules_list');
				$roles_get_rolemodules_list = $this->admin_roles_model->role_data($data);
				$data['roles_get_role'] = $roles_get_role;
				$data['roles_get_rolemodules_list'] = $roles_get_rolemodules_list;
				//echo '<pre>';print_r($data);exit;
				$this->load->view('admin/edit_role_page', $data);
		} */
		
		function edit_role(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('first_name', 'Firstname', 'required|alpha|min_length[3]|max_length[20]');
				$this->form_validation->set_rules('role', 'Role', 'required'); 
				
				if($this->form_validation->run() == FALSE){
				 $this->load->model('admin_roles_model');
				 if($this->uri->segment(4) > 0){
					 $_SESSION['role_id'] = $this->uri->segment(4);
				 }
				$data = array('www-command'=>'roles-get_role','role_id'=>$_SESSION['role_id']);
				$roles_get_role = $this->admin_roles_model->role_data($data);
				$data = array('www-command'=>'roles-get_rolemodules_list');
				$roles_get_rolemodules_list = $this->admin_roles_model->role_data($data);
				$data = array('www-command'=>'roles-role_modules','role_id'=>$_SESSION['role_id']);
				$role_modules = $this->admin_roles_model->role_data($data);
				$data['roles_get_role'] = $roles_get_role;
				$data['roles_get_rolemodules_list'] = $roles_get_rolemodules_list;
				/* foreach($role_modules->result as $module){
					echo '<pre>';print_r($module->module_id);
				}exit; */
				$data['role_modules'] = $role_modules;
				//echo '<pre>';print_r($role_modules);exit;
				$this->load->view('admin/edit_role_page', $data);
				}else{
					$url = 'http://localhost/azindia/azindia-api/json.api';
					$data = array('www-command'=>'roles-edit_role', 
				'first_name' => $this->input->post('first_name'),
				'module_id' => implode(",",$this->input->post('role')),
				'role_id' => $_SESSION['role_id']
				);
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//$data['articles_create_author'] = $response;
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/roles_ctrl/index/');
				}
				}

		function delete_role(){
			$url = 'http://localhost/azindia/azindia-api/json.api';
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'roles-delete_role','status'=>$status,'role_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				
				redirect('admin/roles_ctrl/index/');

		}
	}
	
?>